home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / LDB171.ARJ / MSTR.CPP < prev    next >
C/C++ Source or Header  |  1992-05-12  |  2KB  |  143 lines

  1. /*
  2.     mstr.cpp -- Loose Data Binder v 1.7:
  3.         mutually owned and persistent
  4.         "string form."
  5.  
  6.     (C) Copyright 1992  John W. Small
  7.     All rights reserved
  8.  
  9.     PSW / Power SoftWare
  10.     P.O. Box 10072
  11.     McLean, Virginia 22102 8072 USA
  12.     (703) 759-3838
  13.  
  14.     This file was created by cloning fmutual.cpp
  15.     and
  16.  
  17.     replacing:    with:
  18.  
  19.     CLASS        Mstr
  20.     CPTR        MstR
  21.     BASE        Mutual
  22. */
  23.  
  24.  
  25. #ifndef Mstr_HPP
  26. #include "mstr.hpp"
  27. #endif
  28.  
  29. #include <string.h>
  30.  
  31. int Mstr::initData(char *s, int dup)
  32. {
  33.     this->s = ((dup && s)? strdup(s) : s);
  34.     if (this->s)
  35.         return 1;  // success
  36.     return 0;
  37. }
  38.  
  39. void Mstr::fput(ostream& os)
  40. {
  41.     Mutual::fput(os);
  42.     int i = strlen(s);
  43.     
  44.     os << i << Mendm;
  45.     if (i && os)
  46.         os.write(s,i);
  47.     if (!os)
  48.         error("unable to store Mstr "
  49.             "data on stream");
  50. }
  51.  
  52. MutuaL Mstr::fget(istream& is, MutuaL InstancE)
  53. {
  54.     int newed;
  55.     MstR thiS;
  56.     char * s;
  57.     int i;
  58.  
  59.     is >> i >> Mnextm;
  60.     if (!is)  {
  61.         serror("unable to load Mstr "
  62.             "data from stream",
  63.             ID_Mstr);
  64.         return MutuaL0;
  65.     }
  66.     if ((s = new char[i+i]) != (char *)0)  {
  67.         if (i)  {
  68.             is.read(s,i);
  69.             if (!is)  {
  70.                 delete s;
  71.                 return MutuaL0;
  72.             }
  73.         }
  74.         s[i] = '\0';
  75.     }
  76.     if (InstancE)  {
  77.         newed = 0;
  78.         thiS = (MstR) InstancE;
  79.     }
  80.     else  {
  81.         if ((thiS = new Mstr(initVFTsEtc))
  82.             == MstR0)  {
  83.             serror("unable to construct "
  84.                 "new Mstr for "
  85.                 "loading",
  86.                 ID_Mstr);
  87.             return MutuaL0;
  88.         }
  89.         newed = 1;
  90.         InstancE = (MutuaL) thiS;
  91.     }
  92.     if (!Mutual::fget(is,InstancE))  {
  93.         if (newed)
  94.             delete (voiD) thiS;
  95.         return MutuaL0;
  96.     }
  97.     if (!thiS->initData(s,0))  {
  98.         serror("unable to initialize Mstr "
  99.             "from reloaded stream data",
  100.             ID_Mstr);
  101.         if (newed)
  102.             delete (voiD) thiS;
  103.         return MutuaL0;
  104.     }
  105.     return InstancE;
  106. }
  107.  
  108.  
  109. Mstr::Mstr (char *s)
  110.     : Mutual(initVFTsEtc)
  111. {
  112.     (void) initData(s);
  113. }
  114.  
  115. Mstr::Mstr(Mstr& c) : Mutual(c)
  116. {
  117.     // copy initializer constructor
  118.     if (c.S())
  119.         s = strdup(c.S());
  120.     else
  121.         s = (char *) 0;    
  122. }
  123.  
  124. int Mstr::operator=(Mutual& m)
  125. {
  126.     delete s;
  127.     s = (char *) 0;
  128.     if ((s = ((MstR)&m)->S()) != (char *)0)
  129.         s = strdup(s);
  130.     return 1;  // enabled
  131. }
  132.  
  133. MutuaL Mstr::clone()
  134. {
  135.     // invokes copy initializer constructor
  136.     return (MutuaL) new Mstr(*this);
  137. }
  138.  
  139. Mstr::~Mstr()
  140. {
  141.     delete s;
  142. }
  143.